#include <iostream>
#include <sstream>
#include <cstring>
#include <cerrno>
#include <atomic>
#include <unistd.h>
#include "writer.h"

int htoi(int t)
{
    int u, a, i, j, b;
    u = 0;
    a = 0;
    b = 0;
    i = 0;
    j = 0;
    while (t != 0)
    {
        j = i;
        a = t % 10;
        b = t / 10;
        t = b;
        for (; j > 0; j--)
        {
            a = a * 16;
        }
        u = u + a;
        i++;
    }
    return u;
}

void write_TSNConfig(TSNConfig *tsnconfig, Tunnel *tunnel)
{
    int i = 0;
    int t = 0;
    // std::cout << "2" << std::endl;
    // printf("")
    for (; i < tunnel->TC_num; i++)
    {
        if (!std::strcmp(tunnel->TC[i].entity, "device_id"))
        {
            tsnconfig->device_id = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        }
        else if (!std::strcmp(tunnel->TC[i].entity, "gm_flag"))
        {
            tsnconfig->gm_flag = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        }
        else if (!std::strcmp(tunnel->TC[i].entity, "port_num"))
        {
            tsnconfig->port_num = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        }
        else if (!std::strcmp(tunnel->TC[i].entity, "master_port"))
        {
            tsnconfig->master_port = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        }
        else if (!std::strcmp(tunnel->TC[i].entity, "slave_port"))
        {
            tsnconfig->slave_port = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        }
        else if (!std::strcmp(tunnel->TC[i].entity, "link_delay_measure_period"))
        {
            tsnconfig->link_delay_measure_period = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        }
        else if (!std::strcmp(tunnel->TC[i].entity, "sync_period"))
        {
            tsnconfig->sync_period = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        }
        else if (!std::strcmp(tunnel->TC[i].entity, "domain_number"))
        {
            tsnconfig->domain_number = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        }
        else if (!std::strcmp(tunnel->TC[i].entity, "mean_link_delay_thrld"))
        {
            tsnconfig->mean_link_delay_thrld = std::stoi(tunnel->TC[i].content);
            std::strcpy(tunnel->TC[i].content, "OK");
        }
        else
        {
            std::strcpy(tunnel->TC[i].content, "error");
        }
    }
    int j = 0;
    for (; j < tunnel->PI.ci_num; j++) {
        tsnconfig->port_identity.clockIdentity[j] = static_cast<uint8_t>(std::strtol(tunnel->PI.ci[j].clockIdentity, nullptr, 10));
    }
    // for (; j < tunnel->PI.ci_num; j++)
    // {

    //     t = static_cast<uint8_t>(std::strtol(tunnel->PI.ci[j].clockIdentity, nullptr, 10));
    //     tsnconfig->port_identity.clockIdentity[j] = htoi(t);
    // }
    if (tunnel->PI.content != nullptr)
    {
        tsnconfig->port_identity.portNumber = std::stoi(tunnel->PI.content);
    }
    // if (tunnel->PI.content != nullptr)
    // {

    //     t = std::stoi(tunnel->PI.content);
    //     tsnconfig->port_identity.portNumber = htoi(t);
    // }
}
